//班级负责人下拉框
var xhr = new XMLHttpRequest();
xhr.open("get","http://192.168.192.100:8080/BaorOASys/users/leaderName");
xhr.send();
xhr.onreadystatechange = function (){
    if(xhr.readyState == 4 && xhr.status == 200){
        var res = JSON.parse(xhr.responseText);
        console.log(res);
        var data = res.dataset;
        var classleader = document.getElementById("classleader");
        var classleader1 = document.getElementById("classleader1");
        //添加用户模态框中的班级负责人
        for(var i=0;i<data.length;i++){
            var newoption = document.createElement("option");
            newoption.innerHTML= data[i].userName;
            newoption.setAttribute("value",data[i].userId);
            classleader.appendChild(newoption);
        }
        //修改用户模态框中的班级负责人
        for(var i=0;i<data.length;i++){
            var newoption = document.createElement("option");
            newoption.innerHTML= data[i].userName;
            newoption.setAttribute("value",data[i].userId);
            classleader1.appendChild(newoption);
        }
    }
}


//搜索班级
//点击搜索实现按姓名搜索
var search = document.getElementsByClassName("search")[0];
var btn = search.getElementsByTagName("button")[0];
var input = search.getElementsByTagName("input")[0];
var beginTime = search.getElementsByClassName("beginTime")[0];
var endTime = search.getElementsByClassName("endTime")[0];
btn.onclick = function(){
    console.log(input.value);
    // console.log(beginTime);
    var Json8={
        className:input.value,
        time1:beginTime.value,
        time2:endTime.value,
        pageIndex:1,
        pageSize:5
    }
    ajax.post(ajax.myUrl+"clazz/searchClazz",Json8,function(result){
        console.log(result);
        var data = result.dataset;
        console.log(data);
        if(data.length == 0){
            alert("未找到班级,请重新输入");
        }else{
            var tbody = document.querySelector("tbody");
            tbody.innerHTML = "";
            for(var i=0;i<data.length;i++){
                var newtr = document.createElement("tr");
                newtr.innerHTML=`
                <td>${i+1}</td>
                <td>${data[i].clazzName}</td>
                <td>${data[i].clazzNum}</td>
                <td>${data[i].createTime}</td>
                <td>${data[i].endTime}</td>
                <td>${data[i].clazzLeader}</td>
                <td class="learning">${data[i].clazzStutas}</td>
                <td><div class="remove">修改</div></td>
                <td><div class="detail" id="${result.dataset[i].clazzId}">详情</div></td>
                `;
                tbody.appendChild(newtr);
                // localStorage.setItem("userId"+i,data[i].userId);
            }
        }
        
    // 点击修改，弹出模态框 
    //获取模态框
    var addClass1 = document.getElementsByClassName("addClass1")[0];
    var remove = document.getElementsByClassName("remove");
    console.log(remove);
    console.log(data);
    for(var i=0;i<remove.length;i++){
        remove[i].onclick = function(){
            for(var j=0;j<remove.length;j++){
                if(this == remove[j]){
                    // console.log(j)
                    addClass1.style.display = "block";
                    var clazzid = Number(data[j].clazzId);
                    console.log(clazzid);
                    var xhr = new XMLHttpRequest();
                    xhr.open("get","http://192.168.192.100:8080/BaorOASys/clazz/clazzInfo?clazzId="+clazzid);
                    xhr.send();
                    xhr.onreadystatechange = function (){
                        if(xhr.readyState == 4 && xhr.status == 200){
                            var res = JSON.parse(xhr.responseText);
                            console.log(res);
                            //获取修改班级模态框
                            var addClass1 = document.getElementsByClassName("addClass1")[0];
                            addClass1.querySelector(".classname1").value = res.dataset[0].clazzName;
                            console.log(res.dataset[0].clazzName);
                            // addClass1.querySelector(".classleader").value = res.dataset.clazzName;
                        }
                    }


                    //点击修改班级模态框中的确定或取消，模态框消失
                    var sure1 = addClass1.getElementsByClassName("sure1")[0];
                    sure1.onclick = function(){
                        // console.log(clazzid);
                        var addClass1 = document.getElementsByClassName("addClass1")[0]
                        addClass1.style.display = "none";
                        console.log("11111");
                        //获取修改用户模态框
                        var addClass1 = document.getElementsByClassName("addClass1")[0];
                        var classname1 = addClass1.getElementsByClassName("classname1")[0].value;
                        var classleader1 = document.getElementById("classleader1").value;
                        var begintime1 = addClass1.getElementsByClassName("begintime1")[0].value;
                        var endtime1 = addClass1.getElementsByClassName("endtime1")[0].value;
                        var classleader1 = document.getElementById("classleader1")
                        // console.log(Sex1.value,Dept1.value,Role1.value);
                        var Json7 = {
                            clazzName:classname1,
                            createTime:begintime1,
                            endTime:endtime1,
                            clazzLeader:classleader1.value,
                            clazzId:clazzid,
                            clazzStutas:"已开班"
                        }
                        console.log(Json7);
                        ajax.post(ajax.myUrl+"clazz/updateClazz",Json7,function(result){
                            console.log(result);
                        })
                    }
                    var cancel1 = addClass1.getElementsByClassName("cancel1")[0];
                    cancel1.onclick = function(){
                        addClass1.style.display = "none";
                    }
                }
            }
        }
    }

        //点击班级详情，跳转页面
        var contentBottom = document.getElementsByClassName("contentBottom")[0];
        var detail = contentBottom.querySelectorAll(".detail");
        console.log(detail);
        for(var j=0;j<detail.length;j++){
            detail[j].onclick = function(){
                // console.log(this.id);
                localStorage.setItem("classId",this.id);
                location.href="classDetail.html";
            }
        }




    })
}

var pageCount = 1;
function classOnload(a){
    console.log(a);

//班级列表
var Json = {
    "pageSize":9,
    "pageIndex":a
}
ajax.post(ajax.myUrl+"clazz/clazzList",Json,function(result){
    console.log(result);
    response(result);
    var data = result.dataset.clazzList;
    var tbody = document.querySelector("tbody");
    tbody.innerHTML = ""
    // console.log(tbody);
    // console.log(result.dataset.clazzList);
    for(var i=0;i<result.dataset.clazzList.length;i++){
        var newtr = document.createElement("tr");
        var newtd1 = document.createElement("td");
        newtd1.innerHTML=i+1;
        var newtd2 = document.createElement("td");
        newtd2.innerHTML=result.dataset.clazzList[i].clazzName;
        var newtd3 = document.createElement("td");
        newtd3.innerHTML=result.dataset.clazzList[i].clazzNum;
        var newtd4 = document.createElement("td");
        newtd4.innerHTML=result.dataset.clazzList[i].createTime;
        var newtd5 = document.createElement("td");
        newtd5.innerHTML=result.dataset.clazzList[i].endTime;
        var newtd6 = document.createElement("td");
        newtd6.innerHTML=result.dataset.clazzList[i].clazzLeader;
        var newtd7 = document.createElement("td");
        newtd7.innerHTML=result.dataset.clazzList[i].clazzStutas;
        newtd7.setAttribute("class","learning");
        var newtd8 = document.createElement("td");
        newtd8.innerHTML=`修改`;
        newtd8.setAttribute("class","remove");
        var newtd9 = document.createElement("td");

        newtd9.innerHTML=`详情`;
        newtd9.setAttribute("class","detail");
        newtd9.setAttribute("id",result.dataset.clazzList[i].clazzId)

        newtr.appendChild(newtd1)
        newtr.appendChild(newtd2)
        newtr.appendChild(newtd3)
        newtr.appendChild(newtd4)
        newtr.appendChild(newtd5)
        newtr.appendChild(newtd6)
        newtr.appendChild(newtd7)
        newtr.appendChild(newtd8)
        newtr.appendChild(newtd9)
        tbody.appendChild(newtr);
        
    }

   //点击班级详情，跳转页面
    var contentBottom = document.getElementsByClassName("contentBottom")[0];
    var detail = contentBottom.querySelectorAll(".detail");
    console.log(detail);
    for(var j=0;j<detail.length;j++){
        detail[j].onclick = function(){
            // console.log(this.id);
            localStorage.setItem("classId",this.id);
            location.href="classDetail.html";
        }
    }



    //点击修改，弹出模态框
    //获取模态框
    var addClass1 = document.getElementsByClassName("addClass1")[0];
    var remove = document.getElementsByClassName("remove");
    console.log(remove);
    console.log(data);
    for(var i=0;i<remove.length;i++){
        remove[i].onclick = function(){
            for(var j=0;j<remove.length;j++){
                if(this == remove[j]){
                    // console.log(j)
                    addClass1.style.display = "block";
                    var clazzid = Number(data[j].clazzId);
                    console.log(clazzid);
                    var xhr = new XMLHttpRequest();
                    xhr.open("get","http://192.168.192.100:8080/BaorOASys/clazz/clazzInfo?clazzId="+clazzid);
                    xhr.send();
                    xhr.onreadystatechange = function (){
                        if(xhr.readyState == 4 && xhr.status == 200){
                            var res = JSON.parse(xhr.responseText);
                            console.log(res);
                            //获取修改班级模态框
                            var addClass1 = document.getElementsByClassName("addClass1")[0];
                            addClass1.querySelector(".classname1").value = res.dataset[0].clazzName;
                            console.log(res.dataset[0].clazzName);
                            // addClass1.querySelector(".classleader").value = res.dataset.clazzName;
                        }
                    }


                    //点击修改班级模态框中的确定或取消，模态框消失
                    var sure1 = addClass1.getElementsByClassName("sure1")[0];
                    sure1.onclick = function(){
                        // console.log(clazzid);
                        var addClass1 = document.getElementsByClassName("addClass1")[0]
                        addClass1.style.display = "none";
                        console.log("11111");
                        //获取修改用户模态框
                        var addClass1 = document.getElementsByClassName("addClass1")[0];
                        var classname1 = addClass1.getElementsByClassName("classname1")[0].value;
                        var classleader1 = document.getElementById("classleader1").value;
                        var begintime1 = addClass1.getElementsByClassName("begintime1")[0].value;
                        var endtime1 = addClass1.getElementsByClassName("endtime1")[0].value;
                        var classleader1 = document.getElementById("classleader1")
                        // console.log(Sex1.value,Dept1.value,Role1.value);
                        var Json7 = {
                            clazzName:classname1,
                            createTime:begintime1,
                            endTime:endtime1,
                            clazzLeader:classleader1.value,
                            clazzId:clazzid,
                            clazzStutas:"已开班"
                        }
                        console.log(Json7);
                        ajax.post(ajax.myUrl+"clazz/updateClazz",Json7,function(result){
                            console.log(result);
                        })
                    }
                    var cancel1 = addClass1.getElementsByClassName("cancel1")[0];
                    cancel1.onclick = function(){
                        addClass1.style.display = "none";
                    }
                }
            }
        }
    }

    



})

}

//添加班级

// // 鼠标经过加号显示要添加的内容
var addition = document.getElementsByClassName("addition")[0];
var img = addition.getElementsByTagName("img")[0];
var add = addition.getElementsByClassName("add")[0];
img.onmouseover = function(){
    add.style.display = "block";
}
img.onmouseout = function(){
    add.style.display = "none";
}
//点击加号添加班级，弹出模态框
//获取模态框
var addClass = document.getElementsByClassName("addClass")[0];
img.onclick = function(){
    addClass.style.display = "block";
}
//点击添加班级模态框中的确定或取消，模态框消失
var sure = document.getElementsByClassName("sure")[0];
var addClass = document.getElementsByClassName("addClass")[0];

sure.onclick = function(){
    addClass.style.display = "none";
    var classname = addClass.getElementsByClassName("classname")[0].value;
    var begintime = addClass.getElementsByClassName("begintime")[0].value;
    var endtime = addClass.getElementsByClassName("endtime")[0].value;
    var classleader = document.getElementById("classleader")[0].value;
    var Json6 = {
        clazzName:classname,
        createTime:begintime,
        endTime:endtime,
        clazzLeader:classleader,
        clazzStutas:"已开班"
    }
    console.log(Json6);
    ajax.post(ajax.myUrl+"clazz/addClazz",Json6,function(result){
        console.log(result);
    })
}
var cancel = document.getElementsByClassName("cancel")[0];
cancel.onclick = function(){
    addClass.style.display = "none";
}


